#!/bin/bash
#name:splitFile.sh
# $1 要分割的org文件名 $2 分割后的文件存放的目录
mkdir "$2" 2>/dev/null
count=1
while read line;
do
    filename[$count]="$line"
    let count++
done < <(grep "^\\* " "$1")
csplit "$1" "/^\\* /" -n 4 {*} -f "$2/temp" -b "%04d.part"
tempvar=1
while [ $tempvar -lt $count ];
do
    filenum="`printf \"$2/temp%04d.part\" \"$tempvar\"`"
    newfilename=${filename[$tempvar]}
    newfilename="$2/${newfilename#*\ }.org"
    cat "$2/temp0000.part" "$filenum" > "$newfilename"
    rm "$filenum"
    let tempvar++
done
rm "$2/temp0000.part"
